
void Sweap(SortTypeData* a, SortTypeData* b)
{
	SortTypeData tmp = *a;
	*a = *b;
	*b = tmp;
}


void AdjustDown(SortTypeData* a, int parent, int n)
{
	assert(a);
	int child = parent * 2 + 1;
	while (child < n)
	{
		//找左右孩子中大的
		if (child + 1 < n && a[child] < a[child + 1])
		{
			child++;
		}

		// 交换并迭代向下调整
		if (a[child] > a[parent])
		{
			Sweap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}

		else
		{
			break;
		}
	}

}


void HeapSort(SortTypeData* a, int n)
{
	//建堆
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(a, i, n);
	}

	int end = n-1;
	while (end > 0)
	{
		Sweap(&a[0], &a[end]);
		AdjustDown(a, 0, end);

		end--;
	}

}